//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "StrippedOptionletAdapter.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Termstructures/Volatility/Optionlet/StrippedOptionletBase.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (Cephei::QL::Termstructures::Volatility::Optionlet::IStrippedOptionletBase^ s) : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
    CStrippedOptionletBase^ _Cs;
    try
    {
#ifdef HANDLE
        _phStrippedOptionletAdapter = NULL;
#endif
        _Cs = safe_cast<CStrippedOptionletBase^> (s);
        _Cs->Lock();
        boost::shared_ptr<QuantLib::StrippedOptionletBase>& _s = static_cast<boost::shared_ptr<QuantLib::StrippedOptionletBase>&> (_Cs->GetShared ()); 
        _ppStrippedOptionletAdapter = new boost::shared_ptr<QuantLib::StrippedOptionletAdapter> (new QuantLib::StrippedOptionletAdapter ( _s ));
        SetOptionletVolatilityStructure (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppStrippedOptionletAdapter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cs != nullptr) _Cs->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (boost::shared_ptr<QuantLib::StrippedOptionletAdapter>& childNative, Object^ owner) : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
#ifdef HANDLE
	_phStrippedOptionletAdapter = NULL;
#endif
	_ppStrippedOptionletAdapter = &childNative;
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppStrippedOptionletAdapter));
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (QuantLib::StrippedOptionletAdapter& childNative, Object^ owner) : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
#ifdef HANDLE
	_phStrippedOptionletAdapter = NULL;
#endif
	_ppStrippedOptionletAdapter = new boost::shared_ptr<QuantLib::StrippedOptionletAdapter> (&childNative);
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppStrippedOptionletAdapter));
    _StrippedOptionletAdapterOwner = owner;
    _OptionletVolatilityStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (CStrippedOptionletAdapter^ copy) : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
#ifdef HANDLE
	_phStrippedOptionletAdapter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppStrippedOptionletAdapter = new boost::shared_ptr<QuantLib::StrippedOptionletAdapter> (copy->GetShared());
        _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppStrippedOptionletAdapter));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (System::Type^ t) : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
#ifdef HANDLE
	_phStrippedOptionletAdapter = NULL;
#endif
	if (!t->IsSubclassOf(CStrippedOptionletAdapter::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (QuantLib::Handle<QuantLib::StrippedOptionletAdapter>& childNative, Object^ owner)  : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
	_phStrippedOptionletAdapter = &childNative;
	_ppStrippedOptionletAdapter = &static_cast<boost::shared_ptr<QuantLib::StrippedOptionletAdapter>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppStrippedOptionletAdapter));
    _StrippedOptionletAdapterOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (QuantLib::Handle<QuantLib::StrippedOptionletAdapter> childNative)  : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
	_phStrippedOptionletAdapter = &childNative;
	_ppStrippedOptionletAdapter = &static_cast<boost::shared_ptr<QuantLib::StrippedOptionletAdapter>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppStrippedOptionletAdapter));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::CStrippedOptionletAdapter (QuantLib::StrippedOptionletAdapter childNative)  : COptionletVolatilityStructure(CStrippedOptionletAdapter::typeid)
{
#ifdef HANDLE
	_phStrippedOptionletAdapter = NULL;
#endif
	_ppStrippedOptionletAdapter = new boost::shared_ptr<QuantLib::StrippedOptionletAdapter> (new QuantLib::StrippedOptionletAdapter (childNative));
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppStrippedOptionletAdapter));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::~CStrippedOptionletAdapter ()
{
    if (_ppStrippedOptionletAdapter != NULL)
    {
	    delete _ppStrippedOptionletAdapter;
        _ppStrippedOptionletAdapter = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::!CStrippedOptionletAdapter ()
{
    if (_ppStrippedOptionletAdapter != NULL)
    {
	    delete _ppStrippedOptionletAdapter;
    }
}
QuantLib::StrippedOptionletAdapter& Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::GetReference ()
{
    if (_ppStrippedOptionletAdapter == NULL) throw gcnew NativeNullException ();
	return **_ppStrippedOptionletAdapter;
}
boost::shared_ptr<QuantLib::StrippedOptionletAdapter>& Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::GetShared ()
{
    if (_ppStrippedOptionletAdapter == NULL) throw gcnew NativeNullException ();
	return *_ppStrippedOptionletAdapter;
}
QuantLib::StrippedOptionletAdapter* Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::GetPointer ()
{
    if (_ppStrippedOptionletAdapter == NULL) throw gcnew NativeNullException ();
	return &**_ppStrippedOptionletAdapter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::StrippedOptionletAdapter>& Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::GetHandle ()
{
	if (_phStrippedOptionletAdapter == NULL)
	{
		_phStrippedOptionletAdapter = new Handle<QuantLib::StrippedOptionletAdapter> (*_ppStrippedOptionletAdapter);
	}
	return *_phStrippedOptionletAdapter;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::HasNative () 
{
	return (_ppStrippedOptionletAdapter != NULL);
}

DateTime Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppStrippedOptionletAdapter)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::MaxStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppStrippedOptionletAdapter)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::MinStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppStrippedOptionletAdapter)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::IStrippedOptionletAdapter^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::PerformCalculations::get ()
{
    try
    {
    	(*_ppStrippedOptionletAdapter)->performCalculations ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::IStrippedOptionletAdapter^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppStrippedOptionletAdapter)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Optionlet::IStrippedOptionletAdapter^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionletAdapter_Factory::Create (Cephei::QL::Termstructures::Volatility::Optionlet::IStrippedOptionletBase^ s)
{
    return gcnew CStrippedOptionletAdapter ( s);
}
